<template>
  <div>
    <el-dialog :title="title" :visible="dialogFormVisible" width="75%" @close="handleClose">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label-width="15%" label="合作商名称:" prop="name">
          <el-input v-model="form.name" maxlength="15" show-word-limit placeholder="请输入" />
        </el-form-item>
        <el-form-item label-width="15%" label="联系人:" prop="contact">
          <el-input v-model="form.contact" maxlength="15" show-word-limit placeholder="请输入" />
        </el-form-item>
        <el-form-item label-width="15%" label="联系电话:" prop="mobile">
          <el-input v-model="form.mobile" maxlength="15" show-word-limit placeholder="请输入" />
        </el-form-item>
        <el-form-item label-width="15%" label="分成比例:" prop="ratio">
          <el-input-number v-model="form.ratio" style="width:100%;" controls-position="right" :min="0" :max="10" @blur="handleChange" />
        </el-form-item>
        <el-form-item label-width="15%" label="账号:" prop="account">
          <el-input v-model="form.account" maxlength="15" show-word-limit placeholder="请输入" />
        </el-form-item>
        <el-form-item label-width="15%" label="密码:" prop="password">
          <el-input v-model="form.password" maxlength="15" show-word-limit placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button :loading="loading" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addPointList, editStreet } from '@/api/partner'
import { regionDataPlus } from 'element-china-area-data'

export default {
  props: {
    dialogFormVisible: {
      type: Boolean,
      required: true,
      default: false,
      loading: false
    }
  },
  data() {
    return {
      form: {
        name: '', contact: '', mobile: '', ratio: 0, account: '', password: '' },

      rules: { name: [{ required: true, message: '合作商名称不得为空', trigger: 'blur' }],
        contact: [{ required: true, message: '联系人不得为空', trigger: 'blur' }],
        mobile: [{ required: true, message: '联系电话不得为空', trigger: 'blur' }],
        account: [{ required: true, message: '账号不得为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不得为空', trigger: 'blur' }]
      },
      loading: false,
      options: regionDataPlus,
      selectedOptions: [],
      idd: []
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑点位' : '新增点位'
    },
    submit() {
      return this.form.id ? this.editstreet : this.addstreet
    }
  }, created() {
    console.log(this.form)
  },
  methods: {
    handleClose() {
      this.$emit('update:dialogFormVisible', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '', contact: '', mobile: '', ratio: '', account: '', password: '' }
    }, handleChange() {
      console.log(this.form.ratio)
      // this.form.ratio += '%'
    },
    async addstreet() {
      try {
        await this.$refs.form.validate()
        this.loading = true
        await addPointList(this.form)
        console.log(this.$parent.getList(1))
        this.$message.success('新增成功')
        this.handleClose()
      } catch (e) {
        console.log(e)
      } finally {
        this.loading = false
      }
    },
    async editstreet() {
      try {
        await this.$refs.form.validate()
        this.loading = true
        await editStreet(this.form.id, this.form)
        this.$message.success('编辑成功')
        this.$parent.getList(this.$parent.newdata.pageIndex)
        this.handleClose()
      } catch (e) {
        console.log(e)
      } finally {
        this.loading = false
      }
    }
  }

}
</script>

<style>

</style>
